package com.tensquare.friend.controller;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import com.tensquare.friend.service.NoFriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Macross
 * @Type FriendController.java
 * @Desc
 * @date 2019/12/26 11:16
 */
@RestController
@RequestMapping("/friend")
@Api(value = "交友操作控制器")
public class FriendController {

    @Autowired
    private FriendService friendService;
    @Autowired
    private NoFriendService noFriendService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserClient userClient;

    @PutMapping("/like/{friendId}/{type}")
    @ApiOperation(value = "交友操作",notes = "添加对方为好友或拉黑")
    public Result likeFriend(@PathVariable @ApiParam(value = "对方ID",required = true) String friendId,
                             @PathVariable @ApiParam(value = "1:喜欢 2:拉黑[不喜欢]",required = true) String type){
        //获取jwt鉴权对象Claims
        Claims claims = (Claims) request.getAttribute("claims");
        //判断claims对象是否为空
        if (claims != null) {
            //获取角色信息
            String roles = (String) claims.get("roles");
            //判断角色是否为user
            if (StringUtils.equals("user",roles)) {
                //获取当前用户ID
                String userId = claims.getId();
                //判断是否为添加好友
                if (StringUtils.equals("1",type)) {
                    //添加对方为好友,返回操作结果
                    int effectRow = friendService.addFriend(userId,friendId);
                    //如果操作结果为0,则说明已是好友关系,无需继续往下执行
                    if (effectRow == 0){
                        //返回操作结果
                        return new Result(false,StatusCode.ERROR,"操作失败,你们已是好友关系");
                    }
                    //对方的粉丝数+1
                    Result updateFansCountResult = userClient.updateFansCount(friendId, 1);
                    //用户的关注数+1
                    Result updateFollowCountResult = userClient.updateFollowCount(userId, 1);
                    //如果以上更新操作返回状态都为成功
                    if (updateFansCountResult.isFlag() && updateFollowCountResult.isFlag()) {
                        //返回操作成功
                        return new Result(true,StatusCode.OK,"操作成功");
                    }
                    //返回操作失败
                    return new Result(false,StatusCode.ERROR,"操作失败");
                    //判断是否为拉黑好友
                } else if (StringUtils.equals("0",type)) {
                    //将对方进行拉黑
                    noFriendService.blockFriend(userId,friendId);
                    return new Result(true,StatusCode.OK,"操作成功");
                }
            }
        }
        //返回无权操作
        return new Result(false,StatusCode.ACCESS_ERROR,"无权操作");
    }

    /**
     * 删除好友
     * @param friendId 朋友ID
     * @return
     */
    @DeleteMapping("/{friendId}")
    @ApiOperation(value = "删除好友")
    public Result deleteFriend(@PathVariable @ApiParam(value = "朋友ID",required = true) String friendId){
        //获取jwt鉴权对象Claims
        Claims claims = (Claims) request.getAttribute("claims");
        //判断claims对象是否为空
        if (claims != null) {
            //获取角色信息
            String roles = (String) claims.get("roles");
            //判断角色是否为user
            if (StringUtils.equals("user", roles)) {
                //获取当前用户ID
                String userId = claims.getId();
                //删除还有,返回操作结果
                int effectRow = friendService.delete(userId,friendId);
                if (effectRow == 0) {
                    return new Result(false,StatusCode.ERROR,"操作失败");
                }
                //朋友的粉丝数-1
                Result updateFansCountResult = userClient.updateFansCount(friendId, -1);
                //用户的关注数-1
                Result updateFollowCountResult = userClient.updateFollowCount(userId, -1);
                //如果以上更新操作返回状态都为成功
                if (updateFansCountResult.getCode() == StatusCode.OK && updateFollowCountResult.getCode() == StatusCode.OK) {
                    //返回操作成功
                    return new Result(true,StatusCode.OK,"操作成功");
                }
                //返回操作失败
                return new Result(false,StatusCode.ERROR,"操作失败");
            }
        }
        //返回无权操作
        return new Result(false,StatusCode.ACCESS_ERROR,"无权操作");
    }

}
